#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
articles.py
"""

import random, re, threading, time
import cfg, speech.replies as replies
from util.sitematrix import projects as wikimap, viquimedia
from util.pywikilib import Page, Site, WikiLink, Time, languages
from core.optimizers import printf, fmt
from util.functions import escape_for_regexp, urlencode
from speech import replies

_sites = {}
def switch_sitenames(sn1, sn2, lang):
	sn1 = "http://%s" % sn1
	if lang == "ca":
		sn2 = viquimedia[sn2]
	return sn1, sn2

def arts(bot, evt, cmd):
	""".art &l;<family>&N;:&+g;<lang1>&N;[/&+g;<lang2>&N;] - Compares sites between """\
	"""&l;<family>&N;:&+g;<lang1>&N; and &l;<family>&N;:&+g;<lang2>&N;. If <lang2> is not """\
	"""specified default channel language is assumed."""
	if ":" in cmd.arguments:
		fam = cmd.arguments.split(":", 1)[0]
		cmd.arguments = cmd.arguments.split(":", 1)[1].strip(":")
	else: fam = bot.channels[evt.chan].fam
	sites = cmd.arguments.split("/") if cmd.arguments else None
	printf("articles.py arts() fam($fam) args($1) sites($sites)", cmd.arguments or "", fam=fam, sites=sites)
	if not sites:
		articles = Site(bot.channels[evt.chan].lang, bot.channels[evt.chan].fam).get_articles()
		bot.reply(evt, "We have %s articles... But our articles have more quality. And now let's wikify!", articles)
		return True
	if len(sites)==2:
		print "2 sites", sites
		site1 = Site(sites[0], fam)
		site2 = Site(sites[1], fam)
		articles1=site1.get_articles()
		articles2=site2.get_articles()
	elif len(sites)==1:
		print "1 iste", sites
		if sites[0] != bot.channels[evt.chan].lang:
			site1 = Site(sites[0], fam)
			site2 = Site(bot.channels[evt.chan].lang, fam)
			articles1 = site1.get_articles()
			articles2 = site2.get_articles()
		else:
			articles = Site(bot.channels[evt.chan].lang, fam).get_articles()
			bot.reply(evt, "We have %s articles... But our articles have more quality. And now let's wikify!", articles)
			return True

	if articles1 > articles2 :
		difference = articles1 - articles2
		prep = "&r;over&N;"
	else:
		difference = articles2 - articles1
		prep = "&e;under&N;"
	sitename1, sitename2 = switch_sitenames(site1.sitename(), site2.sitename(), evt.lang)
	bot.reply(evt, u"$1 has $2 articles ($3 $4 $5)",
		sitename1, articles1, difference, prep, sitename2
	)
arts.rule = "art"
arts.thread = True

def get_size(bot, evt, cmd):
	t1 = time.time()
	site = Site(evt.lang, evt.fam)
	page =  Page(site, cmd.arguments)
	info = page.info()
	size = info['length'] if info.has_key("length") else 0
	msg = "[[&l;%s&N;]]" % page.title()
	if size:
		if size>1024:
			msg+= " takes &B;%i&N; bytes &+g;(%i Kb)&N;." % (size, size/1024)
		else:
		   msg+= " takes %i bytes." % (size)
		if info.has_key("redirect"):
		   msg+= " (redirect)"
		msg+= " %f seconds." % (time.time()-t1)
	else:
		msg += bot._translate(evt, " doesn't exist.")
	bot.reply(evt, msg)
get_size.rule = "size"
get_size.chanlang = {"ca": "mida", "es": "tam"}

def fetch(bot, evt, cmd):
	site = WikiLink(cmd.arguments, evt.lang, evt.fam).site()
	if site.sitename() not in _sites:
		_sites[site.sitename()]= site.load_namespaces()
	page = Page(site, cmd.arguments)
	ns = "|".join([escape_for_regexp(ns) for ns in _sites[site.sitename()].namespaces()])
	txt = page.content()
	txt = txt.split("== ")[0]
	txt = re.sub("(?sm)\{\{.*?[\r\n]+\}\}[\r\n]+", "", txt)
	txt = re.sub("(?sm)\{\{.*?\}\}", "", txt)
	txt = re.sub("""(?sm)<ref name="[^"]+" />""", "", txt)
	txt = re.sub("(?sm)<ref[^>]*>.*?</ref>", "", txt)
	txt = re.sub("\[\[.+?:.+?\]\][\r\n]+?", "", txt)
	txt = re.sub("\[\[(?:%s):.+?\]\][\r\n]*?"%ns, "", txt)
	txt = re.sub("\[\[([^|]+?)\]\]", r"&l;\1&N;", txt)
	txt = re.sub("\[\[[^|]+\|([^\]]+?)\]\]", r"&l;\1&N;", txt)
	txt = re.sub("'''([^']+?)'''", r"&B;\1&N;", txt)
	txt = re.sub("''([^']+?)''", r"&+g;\1&N;", txt)
	txt = re.sub("[\r\n]", "", txt)
	if txt:
		txt = txt[:350].split(".")
		if len(txt[-1])<10:
			txt = txt[:-1]
			txt = ".".join(txt) + "."
		elif len(txt[-1])>100:
			txt = ".".join(txt) + u"…"
		else:
			txt = ".".join(txt)
		bot.pub_msg(evt, txt)
fetch.rule = "fetch"

def newarts(bot, evt, cmd):
	""".newarts <site> [-show {bots|anons|users}] [-days <num>] [-r[ecursive]]"""
	wlink = WikiLink(cmd.arguments or "", evt.lang, evt.fam)
	site = wlink.site()

	params = {
		"list": "recentchanges",
		"rcnamespace": 0,
		"rctype": "new",
		"rcprop": ("user", "flags"),
		"rclimit": "max"
	}
	recursive = cmd.opts['r'] or cmd.opts['recursive']
	days = cmd.opts['days'] and cmd.opts['days'].isdigit() and int(cmd.opts['days']) or -1

	rcbegin = Time(Time().subtract(days,"D")) if not recursive else None
	rcend = Time(Time().subtract(days+1,"D"))
	if cmd.opts['all']:
		rcend=None
		rcstart=True
	else:
		rcstart = rcbegin.to_api_oclock() if rcbegin else True
		params['rcend'] = rcend.to_api_oclock()

	articles=0; bot_arts=0; anon_arts=0; users_arts=0
	anon_users=set(); bot_users=set(); loggined_users=set()
	while rcstart:
		if isinstance(rcstart, basestring): params['rcstart'] = rcstart
		data = site.get_api(params)
		articles += len(data['query']['recentchanges'])
		i=0
		for newart in data['query']['recentchanges']:
			i+=1
			if newart.has_key("bot"):
				bot_arts += 1
				bot_users.add(newart['user'])
			elif newart.has_key("anon"):
				anon_arts += 1
				anon_users.add(newart['user'])
			else:
				users_arts += 1
				loggined_users.add(newart['user'])
		rcstart = data['query-continue']['recentchanges']['rcstart'] if data.has_key("query-continue") else None
	diff_users = len(loggined_users) + len(anon_users) + len(bot_users)
	params.update({"site":site.sitename()})

	url = " http://{site}/w/api.php?format=jsonfm&action=query&list={list}&rcnamespace={rcnamespace}" \
		"&rctype={rctype}&rcprop={rcprop}&rclimit={rclimit}"
	if rcbegin:
		params.update({"rcstart":Time(rcbegin.subtract(2)).to_api_oclock()})
		url += "&rcstart={rcstart}"
	if rcend:
		url += "&rcend={rcend}"
	params.update({"rcprop": urlencode({'rcprop': params['rcprop']+tuple(["timestamp"])}).split("=")[1]})

	bot.reply(evt,
		fmt(
			"&B;users:&N; $1;/$5 | &B;anons:&N; $2;/$6 | &B;bots:&N; $3;/$7 | "
			"&B;&l;total:&N; $4;/$8 http://$9/wiki/Special:RecentChanges $10;$11;$12",
			users_arts, anon_arts, bot_arts, users_arts+anon_arts+bot_arts,
			len(loggined_users), len(anon_users), len(bot_users), diff_users,
			site.sitename(),
			"from &B;%s&N;" % rcend.to_string().split(" ")[1] if rcend else "",
			" till &B;%s&N;" % rcbegin.to_string().split(" ")[1] if rcbegin else "",
			cmd.opts['u'] and url.format(**params) or ""
		)
	)
	if cmd.opts["show"]:
		if cmd.opts["show"] == "users":
			editors = loggined_users
		elif cmd.opts["show"] == "anons":
			editors = anon_users
		elif cmd.opts["show"] == "bots":
			editors = bot_users
		bot.reply(evt, "last 10 %s who has edited: %s", cmd.opts["show"], ", ".join(list(editors)[:10]))
newarts.rule = "newarts"
newarts.chanlang = {"ca": "creats"}
newarts.thread = True

def rithm(bot, evt, cmd):
	hours = 24
	tokens = cmd.arguments.split() if cmd.arguments else []
	wikis=""
	if len(tokens)==2:
		for token in tokens:
			if token.strip().isdigit():
				hours = int(token)
				print "HOURS", hours
			elif not token.isdigit():
				wikis = token
				print wikis
	elif len(tokens)==1:
		if tokens[0].isdigit(): hours=int(tokens[0])
		else: wikis=tokens[0]
	print tokens, len(tokens)
	fam=""; rtn = []
	hours = Time().subtract(hours)
	wikis = wikis.split(",")
	for wiki in wikis:
		if not wiki.endswith(":"): wiki = "%s:" % wiki
		if fam and fam not in wiki: wiki= "%s:%s" % (fam, wiki)
		site = WikiLink(wiki, evt.lang, evt.fam).site()
		fam=site.family()
		rcend = Time(hours).to_api()
		rcstart = True
		articles = 0
		params = {
			"list": "recentchanges",
			"rcnamespace": 0,
			"rctype": "new",
			"rcprop": ("user", "flags"),
			"rclimit": "max",
			"rcend": rcend
		}
		while rcstart:
			if isinstance(rcstart, basestring): params['rcstart'] = rcstart
			data = site.get_api(params)
			articles += len(data['query']['recentchanges'])
			rcstart = data['query-continue']['recentchanges']['rcstart'] if data.has_key("query-continue") else None
		rtn.append("&B;%s:&N; %i" %(site._lang, articles))
	bot.reply(evt, " | ".join(rtn))
rithm.rule = "rithm"
rithm.chanlang = {"ca": "ritme", "es": "ritmo"}

def sizes(bot, evt, cmd):
	msg = net.sizes(args)
	thread.start_new_thread(c.privmsg,(canal, msg.encode("utf-8")))
sizes.rule = "sizes"

def interwikis(bot, evt, cmd):
	msg = net.iws(args)
	thread.start_new_thread(c.privmsg,(canal, msg.encode("utf-8")))
interwikis.rule = "iws"

def missing_arts(bot, evt, cmd):
	enarts = net.get_articles(net.site("w:en")[0])['arts']
	f = cs.open("/home/pasqual/public_html/pywikipedia/user_scripts/logs/arts_witout_ca_last.log", "r", "utf-8")
	d = f.read()
	f.close()
	absents, current = d.split(" ",1)[0].split("/")
	article = d.split(" ", 1)[1]
	result = "%0.2f" %(float(enarts)/float(current)*float(absents),)
	falten = ((float(enarts)-float(current))/3)/(24*60*60)
	dies = int(falten)
	hores = (falten - dies) * 24
	minuts = (hores - int(hores)) * 60
	#print "falten", falten
	falten = "falten: %d d %d h %d m" % (dies, int(hores), int(minuts))
	msg = u"(%s / %s) * %s = $E%s$N; %s; %s" % (format_number(enarts), format_number(current), format_number(absents), format_number(result), article, falten)
	c.privmsg(canal, colors(msg).encode("utf-8"))
missing_arts.rule = "missing"
missing_arts.chanlang = {"ca": "absents"}
